﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class CustomerBLL
    {
        private static int customerID = 0;
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectAll()
        {
            return (from p in DB.Customers
                    select new
                    {
                        p.CustomerID,
                        Name=p.LastName+ " "+p.FirstName,
                        p.Address,
                        p.Sex,
                        p.DateOfBirth,
                        p.Tel,
                        p.Email,
                        p.CMND,
                        p.Status
                    }).OrderByDescending(s => s.CustomerID).AsQueryable();
        }

        public string Insert(CustomerDTO customerDTO)
        {
            try
            {
                Customer customer = new Customer();
                customer.CustomerID = (customerID++).ToString();
                customer.FirstName = customerDTO.FirstName;
                customer.LastName = customerDTO.LastName;
                customer.Address = customerDTO.Address;
                customer.Sex = customerDTO.Sex;
                customer.DateOfBirth = customerDTO.DateOfBirth;
                customer.Tel = customerDTO.Tel;
                customer.Email = customerDTO.Email;
                customer.CMND = customerDTO.CMND;
                customer.Status = customerDTO.Status;

                DB.Customers.InsertOnSubmit(customer);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Update(CustomerDTO customerDTO)
        {
            try
            {
                Customer customer = DB.Customers.Where(s => s.CustomerID == customerDTO.CustomerID).FirstOrDefault();
                customer.FirstName = customerDTO.FirstName;
                customer.LastName = customerDTO.LastName;
                customer.Address = customerDTO.Address;
                customer.Sex = customerDTO.Sex;
                customer.DateOfBirth = customerDTO.DateOfBirth;
                customer.Tel = customerDTO.Tel;
                customer.Email = customerDTO.Email;
                customer.CMND = customerDTO.CMND;
                customer.Status = customerDTO.Status;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }            
        }
        public IQueryable SearchIDName(string key)
        {
            int a = 5;

            if (key.Length == 6 && key.ToCharArray()[0].ToString().ToUpper().Equals("C") && MessageAction.IsNumber(key.Substring(1, 5)) && (Convert.ToInt32(key.Substring(1, 5)).GetType() == a.GetType()))
            {
                return DB.Customers.Select(s => new
                {
                    s.CustomerID,
                    Name = s.LastName + " " + s.FirstName,
                    s.Address,
                    s.Sex,
                    s.DateOfBirth,
                    s.Tel,
                    s.Email,
                    s.CMND,
                    s.Status
                }).Where(s => s.CustomerID == key).AsQueryable();
            }
            else
            {
                return DB.Customers.Select(s => new
                {
                    s.CustomerID,
                    Name = s.LastName + " " + s.FirstName,
                    s.Address,
                    s.Sex,
                    s.DateOfBirth,
                    s.Tel,
                    s.Email,
                    s.CMND,
                    s.Status
                }).Where(s => s.Name.Contains(key)).AsQueryable();
            }
        }
    }
}
